<?php

$loader->requireOnce('includes/SearchingCore/SearchException.class.php');
/**
 * Search engine
 * @package local.includes.SearchingCore
 * @author viethd
 * @version $Id$
 * @copyright 2009 LHV Company
 */
class SearchEngine {	
	private $logger ;
	private $sqlExecute;
	
	/**
	 * 
	 * @var ISearchFilter
	 */
	private $searchFilter;
	
	/**
	 * 
	 * @var ISearchResultView
	 */
	private $searchResultView;
	
	
	function __construct() {		
		$this->logger = LoggerManager::getLogger('local.includes.SearchingCore.SearchEngine');		
		$this->sqlExecute = Celini::newOrdo("ORDataObject");
		$this->searchFilter = null;
		$this->searchResultView = null;
	}
	
	/**
	 * Set the filter for search engine.
	 * @param $searchFilter SearchFilter
	 * @throws SearchException if the parameter is not instance of ISearchFilter
	 */
	public function setSearchFilter($searchFilter) {
		if (!($searchFilter instanceof ISearchFilter)) {			
			throw new SearchException('Search Filter is not instance of ISearchFilter');
		}
		$this->searchFilter = $searchFilter;
	}

	/**
	 * Set the result view generator for search engine.
	 * @param $searchResultView SearchResultView
	 * @throws SearchException if the parameter is not instance of ISearchResultView
	 */
	
	public function setSearchResultView($searchResultView) {
		if (!($searchResultView instanceof ISearchResultView)) {
			throw new SearchException('Search Result View is not instance of ISearchResultView');
		}
		$this->searchResultView = $searchResultView;
	}
	
	/**
	 * Search fucntion
	 * @param $filterData
	 * @return array instance obj of class derive from SearchResult
	 */
	public function search($filterData = array()) {
	    $this->logger->info("Start search");
	    $sql = $this->searchFilter->buildSearchFilterSql($filterData);
	    $result = array();
	    if (!is_array($sql) || count($sql) == 0 ) {
	        //return empty result model
	        $this->logger->info("Finish search with empty result model");
	        return array();
	    }
	    $ds = new Datasource_sql();
        $ds->setup($this->sqlExecute->_db, $sql);
        $result = $ds->toArray();
        
	    $result = $this->searchFilter->buildResultModel($result);
	    $this->logger->info("Finish search");
	    return $result;	    	
	}
	
	/**
	 * Render search filter.
	 * @return HTML
	 * @throws SearchException if searchFilter is NULL
	 */
	public function renderFilter() {
		if (is_null($this->searchFilter)) {
			throw new SearchException('Search Filter is NULL');
		}
		return $this->searchFilter->renderSearchFilter();
	}
	
	/**
	 * Render the result to client.
	 * @param $searchResult array of SearchResult
	 * @return HTML
	 * @throws SearchException if searchResultView is NULL
	 */
	public function renderResult($searchResult) {
		if (is_null($this->searchResultView)) {
			throw new SearchException('Search Result View is NULL');
		}
		return $this->searchResultView->renderSearchResult($searchResult);
	}
	
    
}
?>